package com.ynns.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.ynns.springcloud.pojo.Dept;
import com.ynns.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

//提供resultful服务
@RestController
@RequestMapping("/dept")
//Hystrix熔断
public class DeptController {
    @Autowired
    DeptService deptService=null;

    @GetMapping("/queryById/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Dept queryById(@PathVariable("id") Long id){
        Dept dept=deptService.queryById(id);
        if (dept==null){
            throw new RuntimeException("普通----->id=>"+id+",用户不存在，或者信息找不见");
        }
        return dept;
    }
    //备选方案
    public Dept hystrixGet(@PathVariable("id") Long id){
        return new Dept()
                .setDeptno(id)
                .setDname("Server-Hystrix----->id=>"+id+",用户不存在，或者信息找不见")
                .setDb_source("no this database in MySQL");
    }
}
